package com.nis.bcreceiver;

import android.annotation.TargetApi;
import android.app.Notification;
import android.content.IntentFilter;
import android.os.Build;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;

/**
 * Created by Anyway on 2017/2/10.
 */

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class WeChatNotificationListenerService extends NotificationListenerService implements MyReceiver.ServicesOKListener,MyClearReceiver.ServicesClearListener {
    MyClearReceiver clearReceiver;
    @Override
    public void onCreate() {
        super.onCreate();
        myReceiver = new MyReceiver(this);
        registerReceiver(myReceiver, new IntentFilter(MyReceiver.ReceiverKey));
        clearReceiver = new MyClearReceiver(this);
        registerReceiver(clearReceiver, new IntentFilter(MyClearReceiver.ClearKey));
    }

    MyReceiver myReceiver;

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
//        Log.e("after", "包名 " + sbn.getPackageName());
        Notification notification = sbn.getNotification();
        if (notification == null) {
            return;
        }
        if (MainActivity.activity != null) {
            MainActivity.activity.notifyReceive(sbn.getPackageName(), notification);
        }
//        // 当 API > 18 时，使用 extras 获取通知的详细信息
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            Log.e("after", "时间" + notification.when);
//            Bundle extras = notification.extras;
//            if (extras != null) {
//                // 获取通知标题
//                String title = extras.getString(Notification.EXTRA_TITLE, "");
//                // 获取通知内容
//                String content = extras.getString(Notification.EXTRA_TEXT, "");
//                Log.e("after","辩题 和内容  "+title+"   "+content);
//
//            }
//        }
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }

    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
    }

    @Override
    public void setOk() {

        StatusBarNotification[] ss = getActiveNotifications();
        if (ss != null) {
            for (int i = 0; i < ss.length; i++) {
                Notification notification = ss[i].getNotification();
                if (notification == null) {
                    return;
                }
                MainActivity.activity.notifyReceive_show(ss[i].getPackageName(), notification);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myReceiver);
        unregisterReceiver(clearReceiver);
    }

    @Override
    public void canClear() {
        cancelAllNotifications();
    }

}
